<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select allowClear v-model="queryParam.kcyx" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程名称">
              <a-select
                allowClear
                v-model="queryParam.kcdm"
                @search="handleSearch"
                showSearch
                :defaultActiveFirstOption="false"
                :showArrow="false"
                :filterOption="false"
                placeholder="请先选择院系并输入关键词搜索">
                <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{i.kch}}-{{ i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="考试年级"
              >
                <a-select allowClear v-model="queryParam.ksnj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="考试学年"
              >
                <a-select allowClear v-model="queryParam.ksxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试学期"
              >
                <a-select allowClear v-model="queryParam.ksxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试性质"
              >
                <a-select allowClear v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试类别"
              >
                <a-select allowClear v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel" style="margin:0 10px">导出</a-button>
            <a-button @click="queryParam = {}" style="margin:0 10px">重置</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :pageSize="parameter.pageSize"
        :alert="{show: true}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      kcLoading: false,
      advanced: true,
      queryParam: {
        kcdm: ''
      },
      columns: [
        {
          title: '课程代码',
          dataIndex: 'kcdm'
        },
        {
          title: '课程名称',
          dataIndex: 'kcmc'
        },
        {
          title: '成绩段(90-100)',
          dataIndex: 'level1'
        },
        {
          title: '成绩段(80-89)',
          dataIndex: 'level2'
        },
        {
          title: '成绩段(70-79)',
          dataIndex: 'level3'
        },
        {
          title: '成绩段(60-69)',
          dataIndex: 'level4'
        },
        {
          title: '成绩段(60分以下)',
          dataIndex: 'level5'
        },
        {
          title: '总数',
          dataIndex: 'zs'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.scoreStatis(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      list: [],
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0127: state => state.app['TYPE0127'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0109: state => state.app['TYPE0109'],
      TYPE0358: state => state.app['TYPE0358']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0127')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.initSelectMenu()
    // this.getStatis()
  },
  methods: {
    // getStatis () {
    //   this.loading = true
    //   this.$api.cultivate.scoreStatis(this.queryParam).then(res => {
    //     this.list = res.data
    //   }).finally(() => {
    //     this.loading = false
    //   })
    // },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        this.$set(this.queryParam, 'ksnj', res[1].currentGrade)
        this.$set(this.queryParam, 'ksxq', res[1].currentSemester)
        this.$set(this.queryParam, 'ksxn', res[1].schoolYear)
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.queryParam.kcdm = ''
      this.$set(this.selectMenu, 'courseList', [])
      // if (code) {
      //   this.kcLoading = true
      //   this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
      //     this.$set(this.selectMenu, 'courseList', res.data)
      //   }).finally(() => {
      //     this.kcLoading = false
      //   })
      // } else {
      //   this.$set(this.selectMenu, 'courseList', [])
      // }
    },
    // FIXME 课程选择
    fetch(value){
      const code =  this.queryParam.kcyx
      if (code && value && value.length>=2) {
        this.kcLoading = true
        this.$api.cultivate.courseAll({kcssyxm: code, kcmc: value}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    handleSearch (value) {
      console.dir(value)
      this.fetch(value)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    exportExcel () {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$export('/foster/kccj/yfbcjStatisExp', params)
    }
  }
}
</script>

<style>

</style>
